package org.android.kumoh_ing;
 

import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
 
 public class Kumoh_ingActivity extends Activity {
	 private Button mBtnLogin;
	 public static String My_phone = null;
	 public static String My_gmail = null;
	 public static Facebook mFacebook = new Facebook(facebookvalue.FACEBOOK_APP_ID);
   //  public static String Info =null;
//     FileInputStream fis;
 //	 private String Info = null;
 //    private String groupcount = "";
     private static final boolean LoginCheck = true;
     
     public static void setAppPreferences(Activity context, String key, String value) { //accessToken �� �ް� ����
    	 	SharedPreferences pref = null;
    	    pref = context.getSharedPreferences("Kumoh_ingActivity", 0);
    	    SharedPreferences.Editor prefEditor = pref.edit();
    	    prefEditor.putString(key, value);
    	   
    	    prefEditor.commit();
    	 	     
     	}


     public static String getAppPreferences(Activity context, String key) {
    	    String returnValue = null;
    	    SharedPreferences pref = null;
    	    pref = context.getSharedPreferences("Kumoh_ingActivity", 0);
    	    returnValue = pref.getString(key, "");
    	    return returnValue;
    	}
    
    
    
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       facebookvalue.FacebookInstance = mFacebook;
       facebookvalue.mFacebookAccessToken = facebookvalue.FacebookInstance.getAccessToken();

       mFacebook.setAccessToken(facebookvalue.mFacebookAccessToken);
       
       AccountManager mgr = AccountManager.get(this);
       Account[] accts = mgr.getAccounts();
       final int count = accts.length;
       Account acct = null;
       
       TelephonyManager telManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
       My_phone = telManager.getLine1Number();

       for(int i =0; i<count; i++)
       {
       	acct = accts[i];
       	
       	
       	if(acct.type.equals("com.google"))
       	{
       		My_gmail =acct.name;
       	}
       }
          initSetting();
          mBtnEvent();
     }
 
     private void initSetting() {
    	 mBtnLogin = (Button) findViewById(R.id.button1);
     }

 
     private void mBtnEvent() {
         mBtnLogin.setOnClickListener(new OnClickListener() {
        	 
 
             @Override
            public void onClick(View v) {
            	 if(facebookvalue.FACEBOOK_Login_CODE == LoginCheck){
            		setMyFacebookId();
            		Intent intent = new Intent(getBaseContext(), StartActivity.class);
            		startActivity(intent);
 
                  	
                  }
               
                else
                  	Login();          	
             	}
         });

     }
 
     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
     	
        if (requestCode == facebookvalue.FACEBOOK_AUTH_CODE) {
            mFacebook.authorizeCallback(requestCode, resultCode, data);
        }
        
    }

    public class AuthorizeListener implements DialogListener {
        @Override
        public void onComplete(Bundle values) {
        	facebookvalue.FACEBOOK_Login_CODE = true;
        	facebookvalue.mFacebookAccessToken = mFacebook.getAccessToken();
            setAppPreferences(Kumoh_ingActivity.this, "ACCESS_TOKEN", facebookvalue.mFacebookAccessToken);
            //setAppPreferences2(Kumoh_ingActivity.this, "My_phone", facebookvalue.My_phone);
            if (facebookvalue.FacebookD) {
                 Log.v(facebookvalue.LOG_TAG, "::: onComplate :::");
                 
             }
               	setMyFacebookId();
            	Intent intent = new Intent(getBaseContext(), StartActivity.class);
            	startActivity(intent);
          
         	
         }
 
        @Override
         public void onFacebookError(FacebookError e) {
         }
 
         @Override
         public void onError(DialogError e) {
         }
  
      @Override
      public void onCancel() {
       }
     


    }
   

    private void Login(){ 
        mFacebook.authorize(this, new String[] { "offline_access, publish_stream, photo_upload, status_update, share_item, video_upload, create_note, user_photos, email,user_groups, friends_groups, user_about_me" },Facebook.FORCE_DIALOG_AUTH, new AuthorizeListener());
       
    }
    
    private void setMyFacebookId(){
    	try {
			String result = facebookvalue.FacebookInstance.request("me");
			JSONObject MY_nameObj = Util.parseJson(result);
			String myId = MY_nameObj.getString("id");
			setAppPreferences(Kumoh_ingActivity.this, "facebook_myid", myId);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FacebookError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    

}
 